import 'package:flutter/material.dart';
import 'package:plugin_jd_passport_login/plugin_jd_passport_login.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp>
    implements LoginCallBack, SendVerifyCodeCallBack, OneKeyLoginPreCallBack {
  String sUserName = "ceshianquan7";
  String sUserPassword = "ceshi777";
  String phone = "18813152902";

  //移动
  // ignore: non_constant_identifier_names
  String CHINA_MOBILE_APP_ID = "300011945212";

  // ignore: non_constant_identifier_names
  String CHINA_MOBILE_APP_KEY = "C7287DF674A481F0F0660582D5FC0076";

  //联通
  // ignore: non_constant_identifier_names
  String CHINA_UNICOM_APPID = "99166000000000001050";

  // ignore: non_constant_identifier_names
  String CHINA_UNICOM_APPSECRET = "ed22001054d78268bc1367f5432e34bf";

  //电信
  // ignore: non_constant_identifier_names
  String CT_APPID = "8138112402";

  // ignore: non_constant_identifier_names
  String CT_APPSECRET = "l8hXfxXOJQdzeSCAbRrTfFfCGRoE5oZr";

  var verifycodeController = TextEditingController();

  late String preMobile;

  @override
  void initState() {
    super.initState();
    Jdfpassportlogin.init(
      appid: 1215,
      develop: DevelopType.product,
      cmAppId: CHINA_MOBILE_APP_ID,
      cmAppKey: CHINA_MOBILE_APP_KEY,
      cuAppId: CHINA_UNICOM_APPID,
      cuAppKey: CHINA_UNICOM_APPSECRET,
      ctAppId: CT_APPID,
      ctAppKey: CT_APPSECRET,
    );
    preMobile = "";
    Jdfpassportlogin.oneKeyLoginPreGetMobile(callBack: this);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: <Widget>[
            ElevatedButton(
              onPressed: () {
                Jdfpassportlogin.loginWithAccountPassword(
                  account: sUserName,
                  password: sUserPassword,
                  callBack: this,
                );
              },
              child: Text("账号密码登录"),
            ),
            ElevatedButton(
              onPressed: () {
                Jdfpassportlogin.sendVerifyCode(phone: phone, callBack: this);
              },
              child: Text("发送验证码"),
            ),
            TextField(
              controller: verifycodeController,
              decoration: InputDecoration(hintText: '输入验证码'),
            ),
            ElevatedButton(
              onPressed: () {
                Jdfpassportlogin.loginWithVerifyCode(
                    phone: phone,
                    verifycode: verifycodeController.text,
                    callBack: this);
              },
              child: Text("验证码登录"),
            ),
            ElevatedButton(
              onPressed: () {
                Jdfpassportlogin.forgetPassword();
              },
              child: Text("忘记密码"),
            ),
            ElevatedButton(
              onPressed: () {
                Jdfpassportlogin.loginWithWx(
                  weixinId: 'wxe667df8177ee2e32',
                  callBack: this,
                );
              },
              child: Text("微信登录"),
            ),
            ElevatedButton(
              onPressed: () {
                Jdfpassportlogin.loginWithJD(callBack: this);
              },
              child: Text("京东授权登录"),
            ),
            ElevatedButton(
              onPressed: () {
                Jdfpassportlogin.oneKeyLoginWithOperator(
                    callBack: this, securityPhone: preMobile);
              },
              child: Text(preMobile),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void onLoginFail(String error) {
    print("====>loginInfo.error:$error");
  }

  @override
  void onLoginSuccess(LoginInfo loginInfo) {
    print("====>loginInfo.pin:${loginInfo.pin}  a2:${loginInfo.a2}");
  }

  @override
  void onSendVerifyCodeFail(String error) {
    print("====>onSendVerifyCodeFail.error:$error");
  }

  @override
  void onSendVerifyCodeSuccess(int seconds) {
    print("====>onSendVerifyCodeSuccess.seconds:$seconds");
  }

  @override
  void onOneKeyLoginPreFail(String error) {
    print("====>onOneKeyPreFail.error:$error");
  }

  @override
  void onOneKeyLoginPreSuccess(String mobile) {
    print("==============oneKeyLoginPreGetMobile========flutter,val:" + mobile);
    setState(() {
      preMobile = mobile;
    });
  }
}
